package com.jd.service.impl.sercice.impl;

import com.jd.domain.Dept;
import com.jd.mapper.DeptMapper;
import com.jd.service.impl.sercice.IDeptService;
import com.jd.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class DeptServiceImpl implements IDeptService {

    @Override
    public List<Dept> findAll() {
        // 获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.openSession();
        // 根据sqlSession 对象调用mapper方法
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        List<Dept> deptList = mapper.findAll();
        return deptList;
    }

    @Override
    public void batchAddDept(List<Dept> depts) {
        // 获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.openSession();
        // 根据sqlSession对象调用mapper的方法
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        // 调用方法
        mapper.batchAddDept(depts);
        // 提交事务
        sqlSession.commit();
        // 关闭事务
        sqlSession.close();

    }

    @Override
    public void batchDelDept(Long[] ids) {
        // 获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.openSession();
        // 根据sqlSession 对象调用mapper方法
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        // 调用方法
        mapper.batchDelDept(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Dept> findAllDeptAndEmp() {
        // 获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.openSession();
        // 根据sqlSession对象调用mapper的方法
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        List<Dept> allDeptAndEmp = mapper.findAllDeptAndEmp();
        return allDeptAndEmp;
    }
}
